﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGRapidForm.Common;
using System.Collections.Specialized;
using FishWebLib;
using SGRapidForm.Permission.Entity;
using System.Data;

namespace SGRapidForm.Permission.Component
{
    public class ButtonComponent : ComponentBase
    {
        /// <summary>
        /// 添加一条按钮
        /// </summary>
        /// <param name="UserDetails">一条按钮</param>
        /// <returns>是否添加成功</returns>
        public bool AddButton(ButtonEntity button)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Button_ADD", button) > 0);
        }
        /// <summary>
        /// 删除一条按钮
        /// </summary>
        /// <param name="keyid">用户的Keyid</param>
        /// <returns>是否删除成功</returns>
        public bool DeleteButton(string keyid)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Button_Delete", null, keyid) > 0);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="keyids">keyids</param>
        /// <returns>批量删除是否成功</returns>
        public bool BulkDeleteButton(string keyids)
        {
            //Keyid数量
            int count = SqlHelper.GetKeyidsCount(keyids);
            //Keyids转换成Sql语句
            keyids = SqlHelper.ConvertKeyidsToSqlStatement(keyids);

            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Button_BulkDelete", null, keyids) == count);
        }
        /// <summary>
        /// 根据Keyid获取一条按钮
        /// </summary>
        /// <param name="keyid">按钮的Keyid</param>
        /// <returns>按钮集合</returns>
        public ButtonEntity GetButtonByKeyid(string keyid)
        {
            return FishBLLHelper.CallSpGetDataItem<ButtonEntity>("sgrf_permission_Button_GetButtonByKeyid", null, keyid);
        }
        /// <summary>
        /// 根据Keyid获取一条按钮
        /// </summary>
        /// <param name="keyid">按钮的Keyid</param>
        /// <returns>按钮集合</returns>
        public List<ButtonEntity> GetButtonByPageKeyid(string keyid)
        {
            return FishBLLHelper.CallSpGetDataItemList<ButtonEntity>("sgrf_permission_Button_GetPageByKeyid", null, keyid);
        }
        /// <summary>
        /// 更新一条按钮
        /// </summary>
        /// <param name="UserDetails">一条按钮</param>
        /// <returns>是否更新成功</returns>
        public bool UpdateButton(ButtonEntity button)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Button_Update", button) > 0);
        }
        /// <summary>
        /// 查询所有按钮信息 
        /// </summary>
        /// <returns>按钮信息集合</returns>
        public List<ButtonEntity> Button_GetAll()
        {
            return FishBLLHelper.CallSpGetDataItemList<ButtonEntity>("sgrf_permission_Button_GetAll", null);
        }

        /// <summary>
        /// 进行过滤、排序、分页查询用户
        /// </summary>
        /// <param name="startRecord">开始记录索引</param>
        /// <param name="maxRecords">每页记录数</param>
        /// <param name="sortColumns">排序列</param>
        /// <param name="filterConditions">过滤条件SQL</param>
        /// <returns>用户集合</returns>
        public List<ButtonEntity> PagingGetButtons(Int32 startRecord, Int32 maxRecords, String sortColumns, String filterConditions)
        {
            VerifySortColumns(sortColumns);
            if (string.IsNullOrWhiteSpace(sortColumns))
            {
                sortColumns = "[Order] ASC"; 
            }

            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "1", DbType.Int32, " AND 1 ={0} "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "PageKeyid", DbType.String, " AND PageKeyid = '{0}' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "ButtonName", DbType.String, " AND ButtonName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "ButtonCode", DbType.String, " AND ButtonCode like '%{0}%' "));
      
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "ButtonNameEn", DbType.String, " AND ButtonNameEn like '%{0}%' "));
            filterConditions = sql.ToString();

            return FishBLLHelper.CallSpGetDataItemList<ButtonEntity>("sgrf_permission_Button_PagingGetButtons", null, startRecord, maxRecords, sortColumns, filterConditions);
        }

        /// <summary>
        /// 验证排序参数是否包含非法字符，有则抛出异常
        /// </summary>
        /// <param name="sortColumns"></param>
        private void VerifySortColumns(string sortColumns)
        {
            sortColumns = sortColumns.ToLowerInvariant().Replace(" asc", String.Empty).Replace(" desc", String.Empty);
            String[] columnNames = sortColumns.Split(',');

            foreach (String columnName in columnNames)
            {

                switch (columnName.Trim().ToLowerInvariant())
                {

                    case "buttonname":
                    case "buttoncode":
                    case "createdate":
                    case "buttonnameen":
                    case "":
                        break;
                    default:
                        throw new ArgumentException("SortColumns contains an invalid column name.");
                }
            }
        }

        /// <summary>
        /// 进行过滤、排序、分页查询用户记录数
        /// </summary>
        /// <param name="filterConditions">过滤条件</param>
        /// <returns>记录数</returns>
        public int PagingGetButtonCount(String filterConditions)
        {
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "1", DbType.Int32, " AND 1 ={0} "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "PageKeyid", DbType.String, " AND PageKeyid = '{0}' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "ButtonName", DbType.String, " AND ButtonName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "ButtonCode", DbType.String, " AND ButtonCode like '%{0}%' "));

            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "ButtonNameEn", DbType.String, " AND ButtonNameEn like '%{0}%' "));
            filterConditions = sql.ToString();

            return Convert.ToInt32(FishBLLHelper.CallSpExecuteScalar("sgrf_permission_Button_PagingGetButtonCount", null, filterConditions));
        }

        public bool Button_DetectBeforeImporting(string pageCode)
        {
            return Convert.ToBoolean(FishBLLHelper.CallSpExecuteScalar("sgrf_permission_Button_DetectBeforeImporting", null, pageCode));
        }
    }
}
